package com.wxxymaker.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.UUID;
import com.blade.mvc.annotation.MultipartParam;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.multipart.FileItem;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.hook.annotation.AdminRequire;
import com.wxxymaker.model.dao.ArtImageDao;
import com.wxxymaker.utils.ArrayUtils;
import com.wxxymaker.utils.Const;
import com.wxxymaker.utils.FileUtils;
import com.wxxymaker.utils.SimpleCache;

import java.util.*;

/**
 * @Author: Soft
 * @Date: 2018/6/29 17:22
 * @Desc: 文件操作
 */
@AdminRequire
@Path(value = "/file", restful = true)
public class FileController {

    @Inject
    private ArtImageDao imageDao;

    /**
     * 单文件上传
     * @param file 被上上传的文件
     * @param flag 标识 是否直接保存 还是存放在临时目录
     * @return 文件web路径
     */
    @PostRoute("/upload")
    public RestResponse upload(@MultipartParam FileItem file,
                                  @Param(defaultValue = "files") String type,
                                  @Param(defaultValue = "0") int flag) {
        if (file != null) {
            String filename = file.getFileName();
            String fileType = filename.substring(filename.lastIndexOf("."));
            String newName  = UUID.captchaChar(8) + fileType;
            String webPath  = null;

            if (flag == 0) {
                webPath = FileUtils.saveToTemp(file.getData(), newName);
            } else {
                webPath = FileUtils.saveToUpload(file.getData(), type, newName);
            }
            Map<String, Object> fileInfo = new HashMap<>();
            fileInfo.put("filename", newName);
            fileInfo.put("url", webPath);
            return RestResponse.ok(fileInfo);
        }
        return RestResponse.fail();
    }

    @PostRoute("/wangUpload")
    public RestResponse upload_(@MultipartParam FileItem file, @Param String key) {
        if (file != null) {
            String filename = file.getFileName();
            String fileType = filename.substring(filename.lastIndexOf("."));
            String newName  = UUID.captchaChar(8) + fileType;

            String imagePath = FileUtils.saveToUpload(file.getData(), "images", newName);

            if (key != null && !"null".equals(key)) {  // 上传文件先保存在缓存中
                Map<String, List<String>> temps = (Map<String, List<String>>) SimpleCache.get(Const.tempImgKey);
                if (null == temps) {
                    temps = new HashMap<>();
                    temps.put(key, ArrayUtils.of(imagePath));
                } else {
                    List<String> list = temps.get(key);
                    if (null == list){
                        temps.put(key, ArrayUtils.of(imagePath));
                    } else {
                        list.add(imagePath);
                    }
                }
                SimpleCache.set(Const.tempImgKey, temps);
            }
            return RestResponse.ok(imagePath);
        }
        return RestResponse.fail();
    }
}
